package com.efast.dmom.equipment.service.masterdata.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.efast.dmom.equipment.bean.EquipBomBean;
import com.efast.dmom.equipment.dao.masterdata.EquipBomDao;
import com.efast.dmom.equipment.service.masterdata.IEquipBomService;

@Service
public class EquipBomServiceImpl implements IEquipBomService {

	@Autowired
	EquipBomDao bomDao;

	@Override
	public void addSpare(EquipBomBean bomBean) {

		bomDao.addSpareList(bomBean.getParentId(), bomBean.getIds());
	}

	@Override
	public void deleteSpare(EquipBomBean bomBean) {

		bomDao.deleteSpareList(bomBean.getIds());
	}

	@Override
	public void addCategory(EquipBomBean bomBean) {

		bomDao.addCategory(bomBean.getParentId(), bomBean.getIds());
	}

	@Override
	public void deleteCategory(EquipBomBean bomBean) {

		List<Integer> categoryIds = bomBean.getIds();

		bomDao.deleteCategory(categoryIds);

		List<Integer> childList = new ArrayList<Integer>();

		for (int i = 0; i < categoryIds.size(); i++) {

			childList.addAll(bomDao.fetchPartIdsByParentId(categoryIds.get(i)));
		}

		if (childList.size() > 0) {

			EquipBomBean bean = new EquipBomBean();
			bean.setIds(childList);

			deleteParts(bean);
		}
	}

	@Override
	public void deleteParts(EquipBomBean bomBean) {

		List<Integer> partIds = bomBean.getIds();

		bomDao.deleteParts(partIds);

		List<Integer> childList = new ArrayList<Integer>();

		for (int i = 0; i < partIds.size(); i++) {
			childList.addAll(bomDao.fetchSpareIdsByParentId(partIds.get(i)));
		}

		if (childList.size() > 0) {
			bomDao.deleteSpareList(childList);
		}
	}

}
